package com.example.dao;

import com.example.entity.*;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
public interface UserDao extends Mapper<User> {

    //=============================================================
    @Insert("INSERT INTO user (user_id,account, username, password, email, identity)" +
            "VALUES (#{userId},#{account},#{username},#{password},#{email},1)"
    )
    void add(User user);

    @Update("update user set account = #{account},username = #{username},email = #{email} where user_id = #{userId}")
    void update(User user);

    @Delete("delete from user where user_id = #{userId}")
    void deleteById(Integer userId);

    //=======================================================
    @Select("select * from user where account = #{account} limit 1")
    User findByAccount(@Param("account") String account);

    //=============================================
    @Select("select * from user where account = #{account} and password = #{password} and identity = #{identity}")
    User findByAccountAndPaassword(String account, String password,Integer identity);

    @Select("select * from user where user_id = #{userId}")
    User selectById(Integer id);


    List<User> searchUser(@Param("params") Params params);

    List<User> searchBusiness(@Param("params")Params params);

    @Select("select shop.* from shop join user on user.username = shop.username where user.username = #{username}")
    List<ShopInfo> findshop(ShopInfo shopInfo);


    //================================================
    @Select("select count(*) as countnum ,sum(sellnum) as sumnum from tree where username = #{username}")
    ShopInfo findnum(ShopInfo shopInfo);

    //=================================================
    @Update("update user set img = #{img} where user_id = #{userId}")
    void uploadImg(User user);

    @Select("select * from user where user_id = #{userId}")
    User reloadUser(User user);

    //==========================================================
    @Update("update user set password = #{newPassword} where account = #{account}")
    void resetpwd(@Param(value = "account") String account,@Param(value = "newPassword") String newPassword);

    //==============================================================
    List<CheckInfo> findCheckInfo(@Param(value = "params") Params params);

    //===============================================================
    @Update("update `check` set checktime = #{checkTime},checkername = #{checkername},checkstatus = #{checkstatus} where check_id = #{checkId}")
    void checkAgree(Check check);

    //===================================================================
    @Insert("INSERT INTO `check` (check_id,account, username,shopname, email,checktime,checkername,checkstatus)" +
            "VALUES (#{checkId},#{account},#{username},#{shopname},#{email},#{checkTime},#{checkername},0)"
    )
    void storagenewshopper(Check checkinfo);

    //=============================================
    @Select("select * from `check` where check_id = #{checkId}")
    Check findByCheckId(Integer checkId);

    //======================================================
    @Insert("insert into user (user_id, account, username, password, email, identity, img, coin) " +
            "VALUES (#{userId},#{account},#{username},#{password},#{email},2,#{img},#{coin})")
    void addNewUser(User newuser);

    //========================================================
    @Insert("insert into shop (shop_id, shopname, username, createtime) " +
            "VALUES (#{shopId},#{shopname},#{username},#{createtime})")
    void addshop(ShopInfo shopInfo);

    //============================================================
    @Update("update `check` set checktime = #{checkTime},checkername = #{checkername},checkstatus = #{checkstatus} where check_id = #{checkId}")
    void checkReject(Check check);

    //用户下单后暂时扣除用户余额===============================================
    @Update("update user set coin = coin-#{treeMoney} where user_id = #{userId}")
    void updatecoin(Integer userId, int treeMoney);

    //通过username获取用户信息================================================
    @Select("select * from user where username = #{username}")
    User findByUsername(String username);

    //用户取消订单后返还订单金额===============================================
    @Update("update user set coin = coin+#{summoney} where user_id = #{userId}")
    void backUserCoin(Integer userId, int summoney);

    @Select("select count(*) as shopperNumber from user where identity = 2")
    int loadShopperNumber();

    @Delete("delete from user where user_id = #{userId}")
    void deletetUserByUserId(Integer userId);

    //通过商家名称获取商家信息
    @Select("select * from user where username = #{username} and identity = 2")
    User findShopperByUsername(String username);

    //通过交换订单确认收货修改商家余额
    @Update("update user set coin = coin + #{coin} where user_id = #{userId}")
    void updateCoin(Integer userId, int coin);

    //第三种方法:直接使用tk-mybatis自带的包
    // 在UserController 下面直接return uerDao.getAll();
}
